package com.jewelry.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jewelry.entity.Order;
import com.jewelry.service.OrderItemService;
import com.jewelry.service.OrderService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: OrderController
 * Package: IntelliJ IDEA
 * Description:
 *
 * @Author WGH
 * @Create 2024/3/21 21:54
 * @Version 1.0
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    OrderService orderService;

    @Resource
    OrderItemService orderItemService;

    //添加订单
    @PostMapping("/addOrder")
    public Map addOrder(Integer addressId,HttpSession session) {
        Map map = new HashMap();
        //订单添加
        Order order = orderService.addOrder(addressId, session);
        map.put("message","success");
        map.put("order",order);
        map.put("orderId", order.getOrderId().toString());
        return map;
    }

    //删除订单
    @DeleteMapping("/delete/{orderId}")
    public Map deleteOrder(@PathVariable("orderId") Long orderId) {
        Map map = new HashMap();
        orderService.deleteOrder(orderId);
        map.put("message","success");
        return map;
    }

    //修改订单
    @PutMapping("/update/{orderId}/{status}")
    public Map updateOrder(@PathVariable("orderId") Long orderId, @PathVariable("status") String status) {
        Map map = new HashMap();
        String status1 = orderService.updateOrderStatus(orderId, status);
        map.put("message",status1);
        return map;
    }

    //查找订单
    @GetMapping("/select")
    public Map selectOrder(@RequestParam(value = "orderId",defaultValue = "") Long orderId,
                           @RequestParam(value = "userName",defaultValue = "") String userName,
                           @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue= "5")Integer pageSize, HttpSession session) {
        Map map = new HashMap();
        Page<Order> orders = orderService.selectOrder(orderId, userName, pageNum, pageSize, session);
        map.put("orders",orders);
        return map;
    }

    @GetMapping("selectOrderStatus")
    public Map selectOrderStatus(@RequestParam("orderId") Long orderId){
        Order order = orderService.getOne(new QueryWrapper<Order>()
                .eq("order_id", orderId));
        String status = order.getStatus();
        Map map = new HashMap();
        map.put("status",status);
        return map;
    }
}
